/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.mq.common;

import cn.ac.iie.di.wx.mq.configuration.Configuration;
import cn.ac.iie.di.wx.mq.vo.RedisVo;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    //定义运行时参数及其对应的配置环境的变量名
    public static final String cplLogFileName = "cplLogFileName";//恢复日志文件名
    public static final String sourceFileName = "sourceFileName";//筛选的源日志文件名
    public static final String responseFileName = "responseFileName";//筛选的对比日志文件名
        public static final String targetFileName = "targetFileName";//筛选的目标日志文件名
    public static final String starttime = "starttime";
    public static final String endtime = "endtime";
    public static final String corURL = "corURL";
    public static final String groupmediaURL = "groupmediaURL";
    public static final String redis_hosts = "redis_hosts";
    public static final String redis_masterName = "redis_masterName";
    public static final String redis_dbName = "redis_dbName";
    //存放下载信息的队列，用于发送给下载线程进行ftp下载
//    public static LinkedBlockingQueue<FtpDownloadFileVo> ftpDownloadFileVo = new LinkedBlockingQueue<FtpDownloadFileVo>(200);
    public static LinkedBlockingQueue<RedisVo> redisVos = new LinkedBlockingQueue<RedisVo>(200);
    public static LinkedBlockingQueue<RedisVo> mqredisVos = new LinkedBlockingQueue<RedisVo>(200);
    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;
        setKeyAndValue(cplLogFileName, "cplLogFileName");
        setKeyAndValue(sourceFileName, "sourceFileName");
        setKeyAndValue(targetFileName, "targetFileName");
        setKeyAndValue(starttime, "starttime");
        setKeyAndValue(endtime, "endtime");
        setKeyAndValue(corURL, "corURL");
        setKeyAndValue(groupmediaURL, "groupmediaURL");
        //使用添加函数进行运行时参数设置
        setKeyAndValue(redis_hosts, "redis_hosts");
        setKeyAndValue(redis_masterName, "redis_masterName");
        setKeyAndValue(redis_dbName, "redis_dbName");
        setKeyAndValue(responseFileName, "responseFileName");
        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
